//
//  MarketDataCollector.m
//  MarketDataColloctor
//
//  Created by jiro on 10/03/09.
//  Copyright 2010 jiro music. All rights reserved.
//

#import "HtmlCollector.h"
#import "../../Commons.h"
//#import "../../Commons/DbOperator.h"
#import "WebResourceCollector.h"

@implementation HtmlCollector

@synthesize errorMessage;

- (id) init
{
	self = [ super init ];
	errorMessage = [ NSMutableString string ];
	dateIntervalType = DateInterval_Month;
	yahooEnigmaOption = NO;
	htmlEncoding = NSUTF8StringEncoding;
	return self;
}

- (BOOL) collectWithCode0: (NSString*) code0 
	code1: (NSString*) code1
	andMarketDataType: (MarketData::Type) type 
	intoStrings: (NSMutableArray*) htmlStrings
{
	// ---- 各銘柄コードについてhttpクエリを生成 ---- //
	DbOperator* dbOperator = [ DbOperator defaultOperator ];
	NSMutableArray* urls = [ [ NSMutableArray alloc ] init ];

	// ---- ログを取得　---- //
	PGresult* res = nil;
	NSString* sql = [ NSString stringWithFormat:
		@"select * from %@ where %@ = '%@' and %@ = '%@' and %@ = %d;",
		[ DataCollectLog tableName ],
		kText_Code0, code0,
		kText_Code1, code1,
		kText_MarketDataType, type ];
	if ( DbResponse_Success != [ dbOperator executeQuery: sql intoResult: & res ] )
	{
		PQclear( res );
		[ dbOperator disconnect ];
		errorMessage = [ NSString stringWithFormat: 
			@"ログの取得に失敗\nerror: %@\nsql: %@", 
			[ dbOperator errorMessage ], sql ];
		return NO;
	}
	DataCollectLogList* logs = 
		[ DataCollectLogList dataCollectLogListWithPgResult: res ];
	PQclear( res );
	// --------------------------------- //

	// ---- httpクエリを生成 ---- //
	int latestYear;
	int latestMonth;
	int latestDay;
	int currentYear;
	int currentMonth;
	int currentDay;

	//	 最終取得日から今日までのデータを取得
	DataCollectLog* log = [ logs objectAtIndex: 0 ];
	const char* pLatestDate = [ [ [ NSDate dateWithTimeInterval: 3600 * 24 sinceDate: log.latestDate ] description ] 
		cStringUsingEncoding: NSASCIIStringEncoding ];
	sscanf( pLatestDate, "%d-%d-%d ", & latestYear, & latestMonth, & latestDay );
	const char* pCurrentDate = [ [ [ NSDate date ] description ] cStringUsingEncoding: NSASCIIStringEncoding ];
	sscanf( pCurrentDate, "%d-%d-%d ", & currentYear, & currentMonth, & currentDay );

//	NSLog( @"%@ %s ~ %s", code, pLatestDate, pCurrentDate );

	int year = latestYear;
	int month = latestMonth;
	while ( YES )
	{
		if ( year >= currentYear && month >= currentMonth )
		{
			id url = 
				[ self buildWebPageAddressFromYear: year month: month day: 1 
					toYear: year month: month day: currentDay andCode0: code0 code1: code1 ];
			[ urls addObject: url ];
//			NSLog( @"%@", url );
			break;
		}
		else {
			id url =
				[ self buildWebPageAddressFromYear: year month: month day: 1 
					toYear: year month: month day: 31 andCode0: code0 code1: code1 ];			
			[ urls addObject: url ];
//			NSLog( @"%@", url );
		}

		if ( dateIntervalType == DateInterval_Year )
		{
			year++;
			month = currentMonth;
		}
		else
		{
			month++;
			if ( month == 13 )
			{
				year++;
				month = 1;
			}
		}
	}
	// --------------------------------- //
	
	if ( [ urls count ] == 0 )
	{
		[ dbOperator disconnect ];
		return YES;
	}

	// ---- HTMLの取得　-------- //
	WebResourceCollector* collector = 
		[ [ WebResourceCollector alloc ] 
			initWithYahooEnigmaOption: yahooEnigmaOption 
			htmlEncoding: htmlEncoding ];
	[ collector collectContentsOf: urls intoStrings: htmlStrings ];
	if ( [ collector errorMessage ] != nil )
	{	//	エラーログを保存
		[ dbOperator disconnect ];
		[ errorMessage appendString: [ collector errorMessage ] ];
		return NO;
	}
	// ----------------------- //

	[ dbOperator disconnect ];
	return YES;

}

- (NSURL*) buildWebPageAddressFromYear: (int) yearFrom month: (int) monthFrom day: (int) dayFrom 
								toYear: (int) yearTo month: (int) monthTo day: (int) dayTo 
								andCode0: (NSString*) code0 code1: (NSString*) code1
{
	NSException* exception =
	 [ [ NSException alloc ] 
		initWithName: kText_NotImplementedException 
		reason: kText_SelectorIsNotImplemented 
		userInfo: nil ];
	@throw exception;
}

- (BOOL) collectWithCode: (NSString*) code
{
	NSException* exception =
	 [ [ NSException alloc ] 
		initWithName: kText_NotImplementedException 
		reason: kText_SelectorIsNotImplemented 
		userInfo: nil ];
	@throw exception;
}
/*
- (NSArray*) outputUrls: (NSArray*) codes andMarketDataType: (MarketDataType) type
{
	// ---- 各銘柄コードについてhttpクエリを生成 ---- //
	NSMutableDictionary* urlAndSavePaths = [ [ NSMutableDictionary alloc ] init ];
	DbOperator* dbOperator = [ DbOperator defaultOperator ];
	NSString* code;
	NSEnumerator* e = [ codes objectEnumerator ];
	while ( code = [ e nextObject ] )
	{
		// ---- ログを取得　---- //
		PGresult* res = nil;
		NSString* sql = [ NSString stringWithFormat:
			@"select * from %@ where %@ = '%@' and %@ = %d;",
			[ DataCollectLog name ],
			kText_Code, code,
			kText_MarketDataType, type ];
		if ( DbResponse_Success != [ dbOperator executeQuery: sql intoResult: & res ] )
		{
			[ dbOperator disconnect ];
			errorMessage = [ NSString stringWithFormat: 
				@"ログの取得に失敗\nerror: %@\nsql: %@", 
				[ dbOperator errorMessage ], sql ];
			return NO;
		}
		DataCollectLogList* logs = 
			[ DataCollectLogList dataCollectLogListWithPgResult: res ];
		// --------------------------------- //

		// ---- httpクエリを生成 ---- //
		int latestYear;
		int latestMonth;
		int latestDay;
		int currentYear;
		int currentMonth;
		int currentDay;
		//	 最終取得日から今日までのデータを取得
		DataCollectLog* log = [ logs objectAtIndex: 0 ];
		const char* pLatestDate = [ [ log.latestDate description ] cStringUsingEncoding: NSASCIIStringEncoding ];
		sscanf( pLatestDate, "%d-%d-%d ", & latestYear, & latestMonth, & latestDay );
		const char* pCurrentDate = [ [ [ NSDate date ] description ] cStringUsingEncoding: NSASCIIStringEncoding ];
		sscanf( pCurrentDate, "%d-%d-%d ", & currentYear, & currentMonth, & currentDay );

		int year = latestYear;
		int month = latestMonth;
		while ( YES )
		{
			[ urlAndSavePaths 
				setObject: [ NSString stringWithFormat: @"%@/%d-%d-%d.html",
					saveFolder,
					year, month, 1 ]
				forKey: [ self buildWebPageAddressFromYear: year month: month date: 1
							toYear: year month: month date: 31 andCode: code ] ];

			month++;
			if ( month == 13 )
			{
				year++;
				month = 0;
			}
			
			if ( year == currentYear && month == currentMonth )
			{
				break;
			}
		}
		// --------------------------------- //
	}
	// ------------------------------------ //
	
	// ---- set output URL -------- //
	e = [ urlAndSavePaths keyEnumerator ];
	NSString* s;
	NSMutableArray* urls = [ NSMutableArray array ]; 
	while ( s = [ e nextObject ] )
	{
		[ urls addObject: s ];
	}
	// ---------------------------- //

	return urls;	
}
*/

@end
